﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using DHQGHN.PM2.Models;
using Newtonsoft.Json;
using System.Runtime.Serialization.Json;
using System.Net;
using System.IO;
using System.Text;
using DHQGHN.PM2.ViewModels;

namespace DHQGHN.PM2.Controllers
{
    [Authorize]
    public class MappingDiaBanController : BaseController
    {
        log4net.ILog logger = log4net.LogManager.GetLogger(typeof(MappingDiaBanController));

        #region Bind data
        public IList<SelectListItem> BindArea_TK(string areaID)
        {
            try
            {
                List<AreaDSKHHGDModel> TKList = new List<AreaDSKHHGDModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminCode_NotMappingArea");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();

                    string areaDownload = wc.DownloadString(areaDSKHHGDServiceUrl + "GetByAdminCode_NotMappingArea" + "/" + MyUser.AdminCode + "/" + areaID);
                    TKList = JsonConvert.DeserializeObjectAsync<List<AreaDSKHHGDModel>>(areaDownload).Result;
                    foreach (var TK in TKList)
                    {
                        if (TK != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = TK.AREA_DSKHHGD_ID,
                                Text = TK.AREA_NAME
                            });
                    }
                    return lstItems;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        public IList<SelectListItem> BindArea()
        {
            try
            {
                List<AreaModel> TKList = new List<AreaModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminCode");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();

                    string areaDownload = wc.DownloadString(areaServiceUrl + "GetByAdminCode" + "/" + MyUser.AdminCode);
                    TKList = JsonConvert.DeserializeObjectAsync<List<AreaModel>>(areaDownload).Result;
                    foreach (var TK in TKList)
                    {
                        if (TK != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = TK.AREA_ID,
                                Text = TK.AREA_NAME
                            });
                    }
                    return lstItems;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public SelectList BindArea_TK_MappingEnum(string enumID)
        {
            try
            {
                List<AreaModel> TKList = new List<AreaModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminCode_MappingEnum");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();
                    string areaDownload = wc.DownloadString(areaServiceUrl + "GetByAdminCode_MappingEnum" + "/" + MyUser.AdminCode + "/" + enumID);
                    //string areaDownload = wc.DownloadString(areaServiceUrl + "GetByAdminCode_MappingEnum" + "/" + "3433912625" + "/" + enumID);
                    TKList = JsonConvert.DeserializeObjectAsync<List<AreaModel>>(areaDownload).Result;
                    foreach (var TK in TKList)
                    {
                        if (TK != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = TK.AREA_ID,
                                Text = TK.AREA_NAME
                            });
                    }
                    return new SelectList(lstItems, "Value", "Text");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        [AjaxValidateAntiForgeryToken]
        public JsonResult AreaMapped(string enumID)
        {
            try
            {
                return Json(BindArea_TK_MappingEnum(enumID));
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public String BindArea_TK_MappedEnum(string enumID)
        {
            try
            {
                List<AreaModel> TKList = new List<AreaModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminCode_MappingEnum");
                    wc.Encoding = Encoding.UTF8;
                    string Items = String.Empty;

                    string areaDownload = wc.DownloadString(areaServiceUrl + "GetByAdminCode_MappingEnum" + "/" + MyUser.AdminCode + "/" + enumID);
                    //string areaDownload = wc.DownloadString(areaServiceUrl + "GetByAdminCode_NotMappingEnum" + "/" + "3433912625" + "/" + enumID);
                    TKList = JsonConvert.DeserializeObjectAsync<List<AreaModel>>(areaDownload).Result;
                    foreach (var TK in TKList)
                    {
                        if (TK != null)
                            if (TK != TKList.Last())
                                Items += TK.AREA_ID + ",";
                            else
                                Items += TK.AREA_ID;

                    }
                    return Items;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public SelectList BindArea_DS_MappingArea(string areaID)
        {
            try
            {
                List<AreaDSKHHGDModel> DSList = new List<AreaDSKHHGDModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminCode_MappingArea");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();

                    string areaDownload = wc.DownloadString(areaDSKHHGDServiceUrl + "GetByAdminCode_MappingArea" + "/" + MyUser.AdminCode + "/" + areaID);
                    DSList = JsonConvert.DeserializeObjectAsync<List<AreaDSKHHGDModel>>(areaDownload).Result;
                    foreach (var DS in DSList)
                    {
                        if (DS != null)
                            lstItems.Add(new SelectListItem
                            {
                                Value = DS.AREA_DSKHHGD_ID,
                                Text = DS.AREA_NAME
                            });
                    }
                    return new SelectList(lstItems, "Value", "Text");
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [AjaxValidateAntiForgeryToken]
        public JsonResult AreaDS_TKMapped(string areaID)
        {
            try
            {
                return Json(BindArea_DS_MappingArea(areaID));
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public String BindArea_DS_MappedArea(string areaID)
        {
            try
            {
                List<AreaDSKHHGDModel> DSList = new List<AreaDSKHHGDModel>();

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminCode_MappingArea");
                    wc.Encoding = Encoding.UTF8;
                    string Items = String.Empty;

                    string areaDownload = wc.DownloadString(areaDSKHHGDServiceUrl + "GetByAdminCode_MappingArea" + "/" + MyUser.AdminCode + "/" + areaID);
                    DSList = JsonConvert.DeserializeObjectAsync<List<AreaDSKHHGDModel>>(areaDownload).Result;
                    foreach (var DS in DSList)
                    {
                        if (DS != null)
                            if (DS != DSList.Last())
                                Items += DS.AREA_DSKHHGD_ID + ",";
                            else
                                Items += DS.AREA_DSKHHGD_ID;
                    }
                    return Items;
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public SelectList BindGroupEnum()
        {
            List<SelectListItem> lstItems = new List<SelectListItem>();

            lstItems.Add(new SelectListItem
            {
                Text = "Nhóm Tổ trưởng điều tra",
                Value = "TT"
            });
            lstItems.Add(new SelectListItem
            {
                Text = "Nhóm Lập bảng kê",
                Value = "BK",
            });
            lstItems.Add(new SelectListItem
            {
                Text = "Nhóm Vẽ sơ đồ địa bàn",
                Value = "VE"
            });
            lstItems.Add(new SelectListItem
            {
                Text = "Nhóm Điều tra viên",
                Value = "DT"
            });
            lstItems.Add(new SelectListItem
            {
                Text = "Nhóm Nhập liệu viên",
                Value = "NL"
            });
            return new SelectList(lstItems, "Value", "Text");
        }
        [AjaxValidateAntiForgeryToken]
        public JsonResult ddlGroup()
        {
            try
            {
                return Json(BindGroupEnum());
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [AjaxValidateAntiForgeryToken]
        public JsonResult AreaConvertCount(string AreaID)
        {
            MappingDiaBanViewModel mapPost = new MappingDiaBanViewModel();
            mapPost.AREA_DSKHHGD_ID = AreaID;

            List<MappingDiaBanViewModel> mapGet = new List<MappingDiaBanViewModel>();
            int Count;
            try
            {
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "AreaConvertCount");
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(mapPost);
                    string areaConvertDownload = wc.UploadString(areaConvertServiceUrl + "AreaConvertCount", "POST", json);

                    mapGet = JsonConvert.DeserializeObjectAsync<List<MappingDiaBanViewModel>>(areaConvertDownload).Result;
                }

                if (mapGet.Count > 0)
                {
                    Count = mapGet.ToArray()[0].RECODE_COUNT;
                }
                else
                {
                    Count = 0;
                }
                var result = new { Count = Count };
                return Json(result, JsonRequestBehavior.AllowGet);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }

        }
        #endregion

        #region mapping area and area_DSKHHGD

        public string Area_Convert(string areaID)
        {
            try
            {
                List<MappingDiaBanViewModel> areaDSlist = new List<MappingDiaBanViewModel>();
                string areaDS = String.Empty;
                int i = 0;
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAreaID");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();

                    //string enumDownload = wc.DownloadString(enumeratorServiceUrl + "GetByAdminGroupCode" + "/" + MyUser.AdminCode + "/" + GroupCode);
                    string areaDownload = wc.DownloadString(areaDSKHHGDServiceUrl + "GetByAreaID" + "/" + areaID);
                    areaDSlist = JsonConvert.DeserializeObjectAsync<List<MappingDiaBanViewModel>>(areaDownload).Result;

                    foreach (var area in areaDSlist)
                    {
                        i++;
                        if (i < areaDSlist.Count)
                            areaDS += area.AREA_NAME + ", ";
                        else
                            areaDS += area.AREA_NAME;
                    }
                }
                return areaDS;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [AjaxValidateAntiForgeryToken]
        public JsonResult ListAreaDS(string areaID)
        {
            try
            {
                return Json(Area_Convert(areaID));
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult IndexArea_TK_DS(int? page)
        {
            try
            {
                ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
                ViewBag.ActiveMenus[102] = "active";
                ViewBag.ActiveMenus[120] = "active";

                AdminisUnitModel adminList = new AdminisUnitModel();
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByID");
                    wc.Encoding = Encoding.UTF8;
                    List<SelectListItem> lstItems = new List<SelectListItem>();
                    string url = adminUnitServiceUrl + "GetByID" + "/" + MyUser.AdminID;
                    string adminDownload = wc.DownloadString(url);
                    adminList = JsonConvert.DeserializeObjectAsync<AdminisUnitModel>(adminDownload).Result;
                }

                ViewBag.ADM_UNIT = adminList.COMMUNE_NAME + "  -  " + adminList.DISTRICT_NAME + "  -  " + adminList.PROVINCE_NAME;

                List<AreaModel> TKList = new List<AreaModel>();
                GetAreaConvert search = new GetAreaConvert();
                List<MappingDiaBanViewModel> map = new List<MappingDiaBanViewModel>();
                search.CODE = MyUser.AdminCode;
                if (page == null)
                {
                    search.PageIndex = 1;
                }
                else
                    search.PageIndex = page;

                search.PageSize = 10;
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetForMapping");
                    wc.Encoding = Encoding.UTF8;
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(search);
                    string areaDownload = wc.UploadString(areaServiceUrl + "GetForMapping", "POST", json);
                    TKList = JsonConvert.DeserializeObjectAsync<List<AreaModel>>(areaDownload).Result;

                    if (TKList.Count > 0)
                    {
                        double x = (double)TKList.ToArray()[0].RECODE_COUNT / 10;
                        double count = Math.Ceiling(x);
                        ViewBag.Count = count;
                    }
                    else
                        ViewBag.Count = 0;

                    ViewBag.Page = search.PageIndex;
                    foreach (var TK in TKList)
                    {
                        if (TK.AREA_DSKHHGD_NAME != null)
                            map.Add(new MappingDiaBanViewModel
                            {
                                AREA_ID = TK.AREA_ID,
                                AREA_NAME = TK.AREA_NAME,
                                AREA_DSKHHGD_NAME = TK.AREA_DSKHHGD_NAME,
                                RECODE_COUNT = TK.RECODE_COUNT,
                                RowNumber = TK.RowNumber
                            });
                        else
                            map.Add(new MappingDiaBanViewModel
                            {
                                AREA_ID = TK.AREA_ID,
                                AREA_NAME = TK.AREA_NAME,
                                AREA_DSKHHGD_NAME = "Chưa được chuyển đổi địa bàn dân số",
                                RECODE_COUNT = TK.RECODE_COUNT,
                                RowNumber = TK.RowNumber
                            });
                    }
                }
                return View(map);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }
        public ActionResult MappingArea_TK_DS(string areaID, string areaName)
        {
            try
            {
                MappingDiaBanViewModel map = new MappingDiaBanViewModel();
                map.areaSelected = BindArea_DS_MappedArea(areaID);
                map.area = BindArea_TK(areaID);
                map.AREA_ID = areaID;
                map.AREA_NAME = areaName;
                return PartialView(map);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken()]
        public ActionResult IndexArea_TK_DS(MappingDiaBanViewModel map, string command)
        {
            ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
            ViewBag.ActiveMenus[102] = "active";
            ViewBag.ActiveMenus[120] = "active";

            AreaConvertModel ac = new AreaConvertModel();
            ac.AREA_CONVERT_ID = null;
            if (map != null)
            {
                ac.AREA_ID = map.AREA_ID;
                ac.AREA_DSKHHGD_ID_del = map.areaSelected;
                ac.AREA_DSKHHGD_ID_add = map.AREA_DSKHHGD_ID;
            }

            try
            {
                using (WebClient wc = new WebClient())
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        Helper.SetHeaderInfo(wc, "UpdateAreaConvert");
                        DataContractJsonSerializer serializerToUpload = new DataContractJsonSerializer(typeof(AreaConvertModel));
                        serializerToUpload.WriteObject(ms, ac);
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        wc.UploadData(areaConvertServiceUrl + "UpdateAreaConvert", "POST", ms.ToArray());

                        LogStream.Write<MappingDiaBanViewModel>(MyUser.UserName,
                            CommonData.AppName,
                            GetFunction(),
                            "Cập nhật địa bàn - IndexArea_TK_DS", map);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }

            return RedirectToAction("IndexArea_TK_DS");
        }
        #endregion

        #region mapping dia ban va dieu tra vien
        public string Enum_Area(string enumID)
        {
            try
            {
                List<MappingArea_EnumViewModel> enumList = new List<MappingArea_EnumViewModel>();
                string area = String.Empty;
                int i = 0;
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByEnumID");
                    wc.Encoding = Encoding.UTF8;
                    //string enumDownload = wc.DownloadString(enumeratorServiceUrl + "GetByAdminGroupCode" + "/" + MyUser.AdminCode + "/" + GroupCode);
                    string enumDownload = wc.DownloadString(areaEnumServiceUrl + "GetByEnumID" + "/" + enumID);
                    enumList = JsonConvert.DeserializeObjectAsync<List<MappingArea_EnumViewModel>>(enumDownload).Result;

                    foreach (var enumerator in enumList)
                    {
                        i++;
                        if (i < enumList.Count)
                            area += enumerator.AREA_NAME + ", ";
                        else
                            area += enumerator.AREA_NAME;
                    }
                }
                return area;
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [AjaxValidateAntiForgeryToken]
        public JsonResult ListEA(string enumID)
        {
            try
            {
                return Json(Enum_Area(enumID));
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult IndexArea_Enum(MappingArea_EnumViewModel map)
        {
            try
            {
                ViewBag.ActiveMenus = ViewBag.ActiveMenus == null ? new System.Collections.Hashtable() : ViewBag.ActiveMenus;
                ViewBag.ActiveMenus[102] = "active";
                ViewBag.ActiveMenus[109] = "active";

                AdminisUnitModel adminList = new AdminisUnitModel();
                MappingPTDTViewModel mapPost = new MappingPTDTViewModel();
                mapPost.CODE = MyUser.AdminCode;

                List<MappingPTDTViewModel> mapGet = new List<MappingPTDTViewModel>();
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByID");
                    wc.Encoding = Encoding.UTF8;
                    string url = adminUnitServiceUrl + "GetByID" + "/" + MyUser.AdminID;
                    string adminDownload = wc.DownloadString(url);
                    adminList = JsonConvert.DeserializeObjectAsync<AdminisUnitModel>(adminDownload).Result;

                    using (WebClient wc1 = new WebClient())
                    {
                        Helper.SetHeaderInfo(wc1, "SetPTDT_Count");

                        wc1.Encoding = Encoding.UTF8;
                        wc1.Headers["Content-type"] = "application/json; charset=utf-8";
                        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(mapPost);
                        string householdDownload = wc1.UploadString(houseHoldServiceUrl + "SetPTDT_Count", "POST", json);

                        mapGet = JsonConvert.DeserializeObjectAsync<List<MappingPTDTViewModel>>(householdDownload).Result;
                    }
                }
                ViewBag.checkPTDT = false;
                ViewBag.method = "-1";
                if (mapGet.Count > 0)
                {
                    ViewBag.checkPTDT = mapGet.All(s => s.SURVEY_METHOD_ID == mapGet.First().SURVEY_METHOD_ID);
                    ViewBag.method = mapGet.ToArray()[0].SURVEY_METHOD_ID;
                }

                ViewBag.ADM_UNIT = adminList.COMMUNE_NAME + "  -  " + adminList.DISTRICT_NAME + "  -  " + adminList.PROVINCE_NAME;
                return View(map);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult IndexArea_EnumGrid(string GroupCode, int? page)
        {
            try
            {
                List<EnumeratorModel> enumList = new List<EnumeratorModel>();
                SearchEnumerator search = new SearchEnumerator();
                search.ADMIN_CODE = MyUser.AdminCode;
                search.GROUPCODE = GroupCode;
                if (page == null)
                {
                    search.PAGE_INDEX = 1;
                }
                else
                    search.PAGE_INDEX = page;

                search.PAGE_SIZE = 10;

                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "GetByAdminGroupCode");
                    wc.Encoding = Encoding.UTF8;
                    List<MappingArea_EnumViewModel> map = new List<MappingArea_EnumViewModel>();

                    wc.Encoding = Encoding.UTF8;
                    wc.Headers["Content-type"] = "application/json; charset=utf-8";
                    var json = new System.Web.Script.Serialization.JavaScriptSerializer().Serialize(search);
                    string enumDownload = wc.UploadString(enumeratorServiceUrl + "GetByAdminGroupCode", "POST", json);
                    enumList = JsonConvert.DeserializeObjectAsync<List<EnumeratorModel>>(enumDownload).Result;

                    if (enumList.Count > 0)
                    {
                        double x = (double)enumList.ToArray()[0].RECORD_COUNT / 10;
                        double count = Math.Ceiling(x);
                        ViewBag.Count = count;
                    }
                    else
                        ViewBag.Count = 0;

                    ViewBag.Page = search.PAGE_INDEX;
                    foreach (var enums in enumList)
                    {
                        if (enums.AREA_NAME != null)
                            map.Add(
                            new MappingArea_EnumViewModel
                            {
                                ENUMERATOR_ID = enums.ENUMERATOR_ID,
                                AREA_NAME = enums.AREA_NAME,
                                FULL_NAME = enums.FULL_NAME,
                                GROUP = enums.GROUPCODE,
                                ORDER = enums.ORDER,
                                RECORD_COUNT = enums.RECORD_COUNT
                            });
                        else
                            map.Add(
                            new MappingArea_EnumViewModel
                            {
                                ENUMERATOR_ID = enums.ENUMERATOR_ID,
                                AREA_NAME = "Chưa được gán địa bàn",
                                FULL_NAME = enums.FULL_NAME,
                                GROUP = enums.GROUPCODE,
                                ORDER = enums.ORDER,
                                RECORD_COUNT = enums.RECORD_COUNT
                            });
                    }
                    return PartialView(map);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        public ActionResult MappingArea_Enum(string enumID, string enumName, string groupCode)
        {
            try
            {
                MappingArea_EnumViewModel map = new MappingArea_EnumViewModel();
                //map.areaSelected = BindArea_TK_MappingEnum(enumID);
                map.area = BindArea();
                map.areaSelected = BindArea_TK_MappedEnum(enumID);
                map.ENUMERATOR_ID = enumID;
                map.FULL_NAME = enumName;
                map.GROUP = groupCode;
                return PartialView(map);
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken()]
        public ActionResult IndexArea_Enum(MappingArea_EnumViewModel map, string command)
        {
            AreaEnumeratorModel ae = new AreaEnumeratorModel();
            ae.AREA_ENUM_ID = null;
            if (map != null)
            {
                ae.AREA_ID_del = map.areaSelected;
                ae.AREA_ID_add = map.AREA_ID;
                ae.ENUMERATOR_ID = map.ENUMERATOR_ID;
            }
            try
            {
                using (WebClient wc = new WebClient())
                {
                    Helper.SetHeaderInfo(wc, "EditAreaEnumerator");
                    using (MemoryStream ms = new MemoryStream())
                    {
                        DataContractJsonSerializer serializerToUpload = new DataContractJsonSerializer(typeof(AreaEnumeratorModel));
                        serializerToUpload.WriteObject(ms, ae);
                        wc.Headers["Content-type"] = "application/json; charset=utf-8";
                        wc.UploadData(areaEnumServiceUrl + "EditAreaEnumerator", "POST", ms.ToArray());

                        LogStream.Write<MappingArea_EnumViewModel>(MyUser.UserName,
                            CommonData.AppName,
                            GetFunction(),
                            "Phân công cán bộ địa bàn - IndexArea_Enum", map);
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex.ToString());
                throw;
            }
            return RedirectToAction("IndexArea_Enum", map);
        }
        #endregion

        private string GetFunction()
        {
            return "Chuyển đổi địa bàn dân số - MappingDiaBanController";
        }
    }
}